package org.jeecg.config.filter;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 凌乱的枫叶
 * @ClassName StringUtilFilter
 * @Description 过滤工具包
 * @createTime 2022-07-19 10:14:00
 */
@Slf4j
public class StringUtilFilter {

    /**
     * 过滤特殊字符
     * @param str
     * @return
     */
    public static String filterStr (String str){
        String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    /**
     * 过滤emoji表情，将表情替换成*
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        if(source != null) {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|" +
                            "[\ud83e\udd00-\ud83e\uddff]|[\u2300-\u23ff]|[\u2500-\u25ff]|[\u2100-\u21ff]|[\u00a0-\u0fff]|[\u2b00-\u2bff]|[\u2d06]|[\u3030]"
                    ,Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE ) ;
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                source = emojiMatcher.replaceAll("*");
                return source ;
            }
            return source;
        }
        return source;
    }

    /**
     * 过滤表情符号
     * @param content
     * @return
     */
    public static String filterEmoji2(String content) {
        StringBuilder sb = new StringBuilder();
        for (char ch : content.toCharArray()) {
            if (!Character.isHighSurrogate(ch) && !Character.isLowSurrogate(ch)) {
                sb.append(ch);
            }
        }
        if (sb.length() < content.length()) {
            log.info("content filter："+content+ " 过滤前后 "+ content.length() +"|"+ sb.length());
        }
        return sb.toString();
    }
}

